package com.imstlife.vending.adapter;

import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.imstlife.vending.R;

import java.util.List;

public class WiFiItemAdapter extends BaseAdapter {
	private Context context;
	// 得到一个LayoutInfalter对象用来导入布局
	private LayoutInflater mInflater;
	private List<ScanResult> list;
	private List<String> securityModeList;

	/**
	 * 构造函数
	 */
	public WiFiItemAdapter(Context context, List<ScanResult> list,
			List<String> securityModeList) {
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
		this.list = list;
		this.securityModeList = securityModeList;
	}

	@Override
	public int getCount() {
		int count = 0;

		if (list != null) {
			count = list.size();
		}

		return count;
	}

	@Override
	public Object getItem(int position) {
		if (list == null) {
			return null;
		}

		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.list_item_wifi_list, null);
			holder = new ViewHolder();
			// 得到各个控件的对象
			holder.titleTv = (TextView) convertView
					.findViewById(R.id.item_wifi_tv);
			holder.imageView = (ImageView) convertView
					.findViewById(R.id.item_wifi_signal_level_iv);
			holder.secureTv = (TextView) convertView
					.findViewById(R.id.item_wifi_secure);

			convertView.setTag(holder);
		} else {
			// 取出ViewHolder对
			holder = (ViewHolder) convertView.getTag();
		}

		// TODO
		ScanResult scanResult = list.get(position);

		// 热点名称
		holder.titleTv.setText(scanResult.SSID);
		// 热点安全模式
		holder.secureTv.setText(securityModeList.get(position));
		// 链接信号强度
		int strength = WifiManager.calculateSignalLevel(scanResult.level, 5);
		refreshWifi(strength, holder.imageView);

		return convertView;
	}

	/** 存放控件 */
	public final class ViewHolder {
		public ImageView imageView;
		public TextView titleTv;
		public TextView secureTv;
	}

	public void refreshWifi(int rssi, ImageView wifiStateImage) {

		if (rssi == 0) {
			wifiStateImage.setImageResource(R.mipmap.wifi0);
		} else if (rssi == 1) {
			wifiStateImage.setImageResource(R.mipmap.wifi1);
		} else if (rssi == 2) {
			wifiStateImage.setImageResource(R.mipmap.wifi2);
		} else if (rssi == 3) {
			wifiStateImage.setImageResource(R.mipmap.wifi3);
		} else if (rssi == 4) {
			wifiStateImage.setImageResource(R.mipmap.wifi4);
		}
	}

}
